package hot100;

import com.二叉树.练习题.TreeNode;

public class h9_二叉树搜索树中第k小的元素 {
    int res = 0;
    int k = 0;
    void dfs(TreeNode root){
        if(root == null){
            return;
        }
        dfs(root.left);
        if(k == 0){
            return;
        }
        if(--k == 0){
            res = root.val;
        }
        dfs(root.right);
    }
    public int kthSmallest(TreeNode root, int k) {
        this.k = k;
        dfs(root);
        return res;
    }
}
